﻿using System.Data.SqlClient;
using BLL.Models.Administrators;

namespace BLL.Services.Administrators
{
    public class AdministratorsCollection
    {
        private readonly string _connectionString;

        public AdministratorsCollection(string connectionString)
        {
            _connectionString = connectionString;
        }

        public Administrator GetByLogin(string login)
        {
            Administrator result = null;
            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var cmd = StoredProcedures.Administrators.GetByLogin(conn, login);

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var administrator = new Administrator();
                        administrator.Login = reader[0] as string;
                        administrator.Password = reader.GetSqlBytes(1).Value;
                        administrator.Salt = reader.GetSqlBytes(2).Value;
                        result = administrator;
                    }
                }
            }
            return result;
        }
    }
}
